package de.koller.altitude.render.textures;

import de.koller.aframe.gl.ATexture;
import de.koller.aframe.gl.ATexture.ITextureBuilder;
import de.koller.oceanFramework.gl.util.ParticleTexture;
import de.koller.oceanFramework.gl.util.RasterManager;
import de.koller.oceanFramework.util.ColorIntUtil;

public class GridTexture extends ATexture implements ITextureBuilder {

	@Override
	protected void setUp( TextureParameters tparam ) {
		tparam.size = 64;
		tparam.builder = this;
	}

	@Override
	public void build( ParticleTexture texture ) {
		texture.clear( 0xff000000 );
		//texture.clear( 0xffffffff );
		int j = 30;
		//int j = 1;
		
		for( int k = j; k >= 0; k-- ) {
			
			float f = (float)Math.pow( Math.E, -(k+1)/5f ) * 0xff;
			//float f = ( -0.1f*k + 1 ) * 0xff;
			if( f < 0 ) f = 0;
			//float f = 0xff;
			int co = ColorIntUtil.getIntColor( 0xff, (int)f, (int)f, (int)f );
			
			for( int i = 0; i < texture.getWidth(); i++ ) {
				addTo( texture, co, i, k );
				addTo( texture, co, k, texture.getWidth()-1-i );
				addTo( texture, co, texture.getWidth()-1-k, i );
				addTo( texture, co, texture.getWidth()-1-i, texture.getWidth()-k-1 );	
			}
			
			//ImagePreview.showRaster( texture, 500, 500 );
		}
		
	}
	
	private void addTo( RasterManager r, int c, int x, int y ) {
		int cb = r.colorAt( x, y );
		int ca = ColorIntUtil.add( cb, c );
		r.setPixel( ca, x, y );
		
//		int ca = Math.max( ColorIntUtil.getAValue( r.colorAt( x, y ) ), ColorIntUtil.getAValue( c ) );
//		int cr = Math.max( ColorIntUtil.getRValue( r.colorAt( x, y ) ), ColorIntUtil.getRValue( c ) );
//		int cg = Math.max( ColorIntUtil.getGValue( r.colorAt( x, y ) ), ColorIntUtil.getGValue( c ) );
//		int cb = Math.max( ColorIntUtil.getBValue( r.colorAt( x, y ) ), ColorIntUtil.getBValue( c ) );
	}
	
}
